package com.wushi.lenovo.asia5b.app_ui.shopcar.adapter;

import android.text.TextUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mvp.images.WImageLoader;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_model.good.ShopModel;
import com.wushi.lenovo.asia5b.app_model.good.goodbean.GoodsModel;
import com.wushi.lenovo.asia5b.app_ui.shopcar.listener.ShopCarListener;
import com.wushi.lenovo.asia5b.app_widget.mingan.MinganImageView;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

/**
 * Created by zhangyuncai on 2020/3/2.
 * 购物车中商品UI
 */
public class ShopCarPiGoodsAdapter extends BaseQuickAdapter<GoodsModel, BaseViewHolder> {
    private final ShopCarListener shopCarListener;
    private final ShopModel shopModel;

    public ShopCarPiGoodsAdapter(ShopModel shopModel, ShopCarListener shopCarListener) {
        super(R.layout.item_shopcarpi_goodsadapter, shopModel.products);
        this.shopModel = shopModel;
        this.shopCarListener = shopCarListener;
    }

    @Override
    protected void convert(BaseViewHolder helper, GoodsModel item) {
        //商品信息
        setGoodView(helper, item);
        //设置复选框
        setCheckView(helper, item);

        TextView tv_plusview_sub = helper.getView(R.id.tv_plusview_sub);
        TextView tv_plusview_add = helper.getView(R.id.tv_plusview_add);

        helper.getView(R.id.cons_shopcargoods).setOnClickListener(v -> {
            shopCarListener.startGoodsDetailsActivity(item);
        });
        tv_plusview_sub.setOnClickListener(v -> {
            addNum(helper, item, false);
        });
        tv_plusview_add.setOnClickListener(v -> {
            addNum(helper, item, true);
        });
        helper.getView(R.id.iv_shopcargoods_edit).setOnClickListener(v -> {
            shopCarListener.editGoods(item);
        });
        helper.getView(R.id.ll_shopcargoods_delete).setOnClickListener(v -> {
            shopCarListener.deleteGoods(item);
        });
    }

    private Disposable addNumDisposable;

    private void addNum(BaseViewHolder helper, GoodsModel item, boolean isAdd) {
        try {
            int doubleNum = 1;//倍数,>1有效
            if (item.getSell_case_value() > 1) {
                doubleNum = item.getSell_case_value();
            }
            //数量
            EditText et_plusview_num = helper.getView(R.id.et_plusview_num);
            int currentNum = 0;
            String currentNumStr = et_plusview_num.getText().toString().trim();
            if (!TextUtils.isEmpty(currentNumStr)) {
                currentNum = Integer.parseInt(currentNumStr);
            }
            currentNum = isAdd ? currentNum + doubleNum : currentNum - doubleNum;
            if (currentNum % doubleNum != 0) {//必须是倍数关系
                currentNum = currentNum / doubleNum * doubleNum;
            }
            if (currentNum < doubleNum) {
                currentNum = doubleNum;
            }

            et_plusview_num.setText(currentNum + "");

            //延时请求接口
            if (addNumDisposable != null && !addNumDisposable.isDisposed()) {
                addNumDisposable.dispose();
            }
            int finalCurrentNum = currentNum;
            addNumDisposable = Observable.timer(300, TimeUnit.MILLISECONDS)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(aLong -> {
                        shopCarListener.addGoods(item, finalCurrentNum);
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置复选框信息
     *
     * @param helper
     * @param item   商品数据
     */
    private void setCheckView(BaseViewHolder helper, GoodsModel item) {
        //复选框
        ImageView iv_shopcargoods_check = helper.getView(R.id.iv_shopcargoods_check);
        if (item.canCheck()) {
            iv_shopcargoods_check.setImageResource(item.isChecked() ? R.drawable.ic_check_press : R.drawable.ic_check_normal);
            if (!shopCarListener.isDaigouGoods()) {//代购
                if (item.isChecked() && item.isMingan()) {
                    iv_shopcargoods_check.setImageResource(R.drawable.ic_payment_s);
                }
            }
            iv_shopcargoods_check.setOnClickListener(v -> {
                shopCarListener.selectGoods(shopModel, item);
            });
        } else {
            iv_shopcargoods_check.setImageResource(R.drawable.ic_order_unclickcheck);
            iv_shopcargoods_check.setOnClickListener(v -> {

            });
        }
    }


    /**
     * 设置商品信息
     *
     * @param helper
     * @param item   商品数据
     */
    private void setGoodView(BaseViewHolder helper, GoodsModel item) {
        //商品名
        TextView tv_shopcar_goodname = helper.getView(R.id.tv_shopcargoods_goodname);
        tv_shopcar_goodname.setText(item.getName());

        //商品图片
        MinganImageView mingan_shopcarpi = helper.getView(R.id.mingan_shopcargoods);
        WImageLoader.displayImage(mContext, item.getImage(), mingan_shopcarpi.getImageView());
        //是否显示敏感
        if (shopCarListener.isDaigouGoods()) {
            mingan_shopcarpi.setMinganTextVisibility(false);
        } else {
            mingan_shopcarpi.setMinganTextVisibility(item.isMingan());
        }

        //商品属性
        TextView tv_shopcar_attr = helper.getView(R.id.tv_shopcargoods_attr);
        if (!item.canCheck()) {
            tv_shopcar_attr.setText(R.string.shopcar_attrnull);
        } else {
            tv_shopcar_attr.setText(item.getAttr_name());
        }

        //备注
        TextView tv_shopcar_remark = helper.getView(R.id.tv_shopcargoods_remark);
        tv_shopcar_remark.setText(item.getRemark());

        //价格
        TextView tv_shopcar_price = helper.getView(R.id.tv_shopcargoods_price);
        tv_shopcar_price.setText(item.getTotal_text());

        //数量
        EditText et_plusview_num = helper.getView(R.id.et_plusview_num);
        et_plusview_num.setText(item.getQuantity() + "");
    }
}
